﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ProtocolConversion_WINCE
{
    class Converter
    {
        /// <summary>
        /// 转换整形数据网络次序的字节数组
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        public static byte[] Int32ToBytes(UInt32 i)
        {
            byte[] t = BitConverter.GetBytes(i);
            byte b = t[0];
            t[0] = t[3];
            t[3] = b;
            b = t[1];
            t[1] = t[2];
            t[2] = b;
            return (t);
        }

        public static byte[] IntToBytes(uint source, int number)
        {
            byte[] t = new byte[number];
            t = BitConverter.GetBytes(source);
            byte temp;
            for (int i = t.Length - 1; i > t.Length / 2; i--)
            {
                temp = t[i];
                t[i] = t[t.Length - 1 - i];
                t[t.Length - 1 - i] = temp;
            }
            return (t);
        }

        public static byte[] ShortToBytes(short source, int number)
        {
            byte[] t = new byte[number];
            t = BitConverter.GetBytes(source);
            byte temp;
            for (int i = t.Length - 1; i > t.Length / 2; i--)
            {
                temp = t[i];
                t[i] = t[t.Length - 1 - i];
                t[t.Length - 1 - i] = temp;
            }
            return (t);
        }

        /// <summary>
        /// 返回字节数组代表的整数数字，4个数组
        /// </summary>
        /// <param name="bs"></param>
        /// <param name="startIndex"></param>
        /// <returns></returns>
        public static UInt32 BytesToUInt32(byte[] bs, int startIndex)
        {
            byte[] t = new byte[4];
            for (int i = 0; i < 4 && i < bs.Length - startIndex; i++)
            {
                t[i] = bs[startIndex + i];
            }

            byte b = t[0];
            t[0] = t[3];
            t[3] = b;
            b = t[1];
            t[1] = t[2];
            t[2] = b;

            return BitConverter.ToUInt32(t, 0);
        }

 

        public static short BytesToShort(byte[] bs, int startIndex)
        {
            byte[] t = new byte[2];
            for (int i = 0; i < 2 && i < bs.Length - startIndex; i++)
            {
                t[i] = bs[startIndex + i];
            }
            byte b = t[0];
            t[0] = t[1];
            t[1] = b;
            return BitConverter.ToInt16(t, 0);
        }

        public static uint BytesToUInt(byte[] b, int startIndex, int number)
        {
            byte[] t = new Byte[number];
            for (int i = 0; i < number && i < b.Length - startIndex; i++)
            {
                t[i] = b[startIndex + i];
            }

            byte temp;
            for (int i = t.Length - 1; i > t.Length / 2; i--)
            {
                temp = t[i];
                t[i] = t[t.Length - 1 - i];
                t[i] = temp;
            }
            return (BitConverter.ToUInt32(t, 0));
        }
        /// <summary>
        /// 取数据
        /// </summary>
        /// <param name="b"></param>
        /// <param name="startIndex"></param>
        /// <param name="number"></param>
        /// <returns></returns>
        public static Int16 BytesToUInt16(byte[] b, int startIndex)
        {
            byte[] t = new Byte[2];
            for (int i = 0; i < 2 && i < b.Length - startIndex; i++)
            {
                t[i] = b[startIndex + i];
            }

            byte temp;
            for (int i = t.Length - 1; i > t.Length / 2; i--)
            {
                temp = t[i];
                t[i] = t[t.Length - 1 - i];
                t[i] = temp;
            }
            return (BitConverter.ToInt16(t, 0));
        }

        public static UInt32 BytesToUInt32(byte[] b, int startIndex, int number)
        {
            byte[] t = new Byte[number];
            for (int i = 0; i < number && i < b.Length - startIndex; i++)
            {
                t[i] = b[startIndex + i];
            }

            byte temp;
            for (int i = t.Length - 1; i > t.Length / 2; i--)
            {
                temp = t[i];
                t[i] = t[t.Length - 1 - i];
                t[i] = temp;
            }
            return (BitConverter.ToUInt32(t, 0));
        }
        /// <summary>
        /// 转换数据
        /// </summary>
        /// <param name="receiveDate">收到的数据</param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <returns></returns>
        public static byte[] TakeOutByte(byte[] b, int startIndex, int number)
        {

            byte[] t = new Byte[number];
            for (int i = 0; i < number && i < b.Length - startIndex; i++)
            {
                t[i] = b[startIndex + i];
            }

            byte temp;
            for (int i = t.Length - 1; i > t.Length / 2; i--)
            {
                temp = t[i];
                t[i] = t[t.Length - 1 - i];
                t[i] = temp;
            }
            return t;
        }
        public static byte[] TakeOutByteNoOrder(byte[] b, int startIndex, int number)
        {
            byte[] t = new Byte[number];
            for (int i = 0; i < number && i < b.Length - startIndex; i++)
            {
                t[i] = b[startIndex + i];
            }
            return t;
        }
        /// <summary>
        /// 没有指定起始索引
        /// </summary>
        /// <param name="bs"></param>
        /// <returns></returns>
        //public static uint BytesToUInt(byte[] bs) 
        //{
        //    return (BytesToUInt32(bs,0));
        //}
    }

    /// <summary>
    /// 缓冲区对象
    /// </summary>
    public class BufferObject
    {
        private byte[] buffer = null;
        private int length = 0;

        public BufferObject(byte[] bytes, int len)
        {
            if (buffer != null)
            {
                buffer = null;
                GC.Collect();
            }

            length = len;
            buffer = new byte[len];
            for (int i = 0; i < len; i++)
            {
                buffer[i] = bytes[i];
            }
        }

        public byte[] Buffer
        {
            get { return buffer; }
        }

        public int Length
        {
            get { return length; }
        }
    }
}
